package com.demo.controller;

import com.demo.entity.Employee;
import com.demo.service.api.EmployeeRemoteService;
import com.demo.util.ResultEntity;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @author Stone
 * @date 2020/6/18 12:24
 *  1.实现consumer端降级功能
 *  2.并且在@FeignClient传入MyFallbackFactory
 *  3.当公共接口EmployeeRemoteService调用发生异常或超时，就要通过Hystrix降级机制找到FallbackFactory，执行里面的create（）
 */
@Component
public class MyFallbackFactory implements FallbackFactory<EmployeeRemoteService> {
    @Override
    public EmployeeRemoteService create(final Throwable cause) {
        return new EmployeeRemoteService() {
            @Override
            public Employee getEmployeeRemote() {
                return null;
            }

            @Override
            public ResultEntity<Employee> getEmp(String keyword) {
                return ResultEntity.failed(cause.getMessage());
            }
        };
    }
}
